package com.example.hotelmanager.controller;

import com.example.hotelmanager.entity.Cost;
import com.example.hotelmanager.entity.vo.Response;
import com.example.hotelmanager.entity.vo.ResponseMsg;
import com.example.hotelmanager.service.CostService;
import com.example.hotelmanager.service.CostTypeService;
import com.example.hotelmanager.service.FrontService;
import com.example.hotelmanager.utils.JwtUtill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/30 18:24
 */
@RestController
@Slf4j
@CrossOrigin
public class CostController {
    @Autowired
    private CostService costService;
    @Autowired
    private FrontService frontService;
    @Autowired
    private JwtUtill jwtUtill;

    @PostMapping("/addCost")
    public Response addCost(HttpServletRequest request, int id, int costTypeId, String roomId){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            Cost cost = new Cost(id,costTypeId,roomId,1,0);
            costService.saveOrUpdate(cost);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }



    @DeleteMapping("/deleteCost")
    public Response deleteCost(HttpServletRequest request,Integer id){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            costService.removeById(id);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    @GetMapping("/getCostById")
    public Response getCostById(HttpServletRequest request,Integer id){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null || frontService.getById(num) != null){
            Cost cost = costService.getById(id);
            if(cost != null ){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("cost",cost);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    /**
     * 根据roomId查看全部消费记录，计算已结算和未结算的金额和总金额
     * @param request
     * @return
     */
    @PostMapping("/getCostByRoomId")
    public Response getCostByRoomId(HttpServletRequest request,String roomId){
        String num = (String) request.getAttribute("num");
        if( frontService.getById(num) != null){
            Map<String, Object>resultMap=costService.getCostByRoomId(roomId);
            if(resultMap!=null){
                String token = jwtUtill.updateJwt(num);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
    /**
     * 根据roomId结算
     * @param request
     * @return
     */
    @PostMapping("/settleCostByRoomId")
    public Response settleCostByRoomId(HttpServletRequest request,String roomId){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            //将未结算的设置成已结算的
            costService.settleCostByRoomId(roomId);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }


    /**
     * 结算单条消费记录
     * @param request
     * @return
     */
    @PostMapping("/settleCostById")
    public Response settleCostById(HttpServletRequest request,int id){
        String num = (String) request.getAttribute("num");
        if(frontService.getById(num) != null){
            Cost cost = costService.getById(id);
            if(cost != null){
                cost.setState(1);
                costService.saveOrUpdate(cost);
                String token = jwtUtill.updateJwt(num);
                return (new Response()).success(token);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }


}
